package com.gxa.p2p.service.impl;


import com.gxa.p2p.mapper.ArticleMapper;
import com.gxa.p2p.pojo.Article;
import com.gxa.p2p.pojo.Management;
import com.gxa.p2p.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 文章操作业务层实现
 * @author HS
 */
@Transactional(rollbackFor = {Throwable.class})
@Service("articleService")
public class ArticleServiceImpl implements ArticleService {

    @Autowired
    private ArticleMapper mapper=null;


    @Override
    public Article findArticleById(Long id) {
        return  mapper.findArticleById(id);
    }

    @Override
    public Map<Integer,List<Article>> findArticleByPage(Map map) {
        //得到模糊查询的经分页的数据
        Boolean state=null;
        if(map.get("state")!=null&&!map.get("state").toString().equals("")){
            state=Boolean.valueOf(map.get("state").toString());
        }

        map.put("state",state);

        Integer page = Integer.parseInt(map.get("page").toString()) ;
        Integer size = Integer.parseInt(map.get("size").toString());

        map.put("page",(page-1)*size) ;
        map.put("size",size) ;
        List<Article> resultList= mapper.findArticleByPage(map);
        //得到数据总数count
        map.put("page",null);
        map.put("size",null);

        Integer count = mapper.findArticleByPage(map).size();

        Map<Integer,List<Article>> resultMap=new HashMap<>();
        resultMap.put(count,resultList);

        return resultMap;
    }

    @Override
    public void addArticle(Article article) {

        mapper.addArticle(article);
    }

    @Override
    public void deleteArticle(Long id) {
        mapper.deleteArticle(id);
    }

    @Override
    public void updateArticle(Article article) {
        mapper.updateArticle(article);
    }
}
